package com.charon.auth.demo.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
  *@ClassName: TimeFilter
  *@Description: 过滤器（可以拿得到原始的Http请求的信息，但是拿不到处理的方法的信息。）
  *@Author: Charon [248135724@qq.com]
  *@Date: 2019/1/13 0013 0:03
  *@Version: 1.0
 *
 * 执行顺序：Filter=>Intercept=>ControllerAdvice（注解：只在返回抛出异常时走这里）
 *              =>Aspect=>Controller
  */
@Slf4j
@Component
public class TimeFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("初始化过滤器：TimeFilter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        long begin = System.currentTimeMillis();
        log.info("过滤器开始执行："+begin);
        //request.setAttribute("startTime",begin);
        chain.doFilter(request,response);
        log.info("过滤器结束-耗时："+(System.currentTimeMillis()-begin));
    }

    @Override
    public void destroy() {

    }
}
